package movie.robot;

import movie.wrapper.Caching;
import movie.wrapper.Methods;
import movie.wrapper.Movie;

import java.util.*;
import com.google.wave.api.*;

//Needs cleaning up
public class MovieRobotServlet extends AbstractRobotServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final String COMMAND = "@movie ";
	private final String COMMAND_MOVIE = "@moviename ";
	private final String COMMAND_ARTIST = "@artist ";
	private final String COMMAND_CLEAR_CACHE = "@moviecc now";
	private ArrayList<String> userAccess = new ArrayList<String>();
	
	//easter egg
	private final String COMMAND_KILL = "@moviekill";
	
	//Constructor
	public MovieRobotServlet()
	{
		userAccess.add("dcumbo@googlewave.com");
		userAccess.add("bengatt@googlewave.com");
		userAccess.add("dean.clatworthy@googlewave.com");
	}
	
	@Override
	public void processEvents(RobotMessageBundle bundle) 
	{
		Wavelet wavelet = bundle.getWavelet();
		
		if(bundle.wasSelfAdded())
		{
			//Initial Blip, notify user and give basic Robot info
			Blip blip = wavelet.appendBlip();
			TextView textView = blip.getDocument();
			textView.append("NOTE: THIS IS STILL IN DEVELOPMENT AND IF YOU'RE NOT A DEVELOPER IT WON'T WORK.\n\rMovie Robot has been added to the wave. Please use command " + COMMAND +" [movie name] or "+ COMMAND_MOVIE +" [movie name] to get results");
		}
		
		for (Event e: bundle.getEvents())
		{
			if (e.getType() == EventType.BLIP_SUBMITTED)
			{			
				String blipSubmitter = e.getBlip().getCreator();
				if (!userAccess.contains(blipSubmitter))
				{
					return;
				}
				
				//Get submitted blip
				Blip blip = e.getBlip();
				TextView textView = blip.getDocument();
				
				//Getting user input
				String userInput = textView.getText();
				
				if(userInput.toLowerCase().startsWith(COMMAND) || userInput.toLowerCase().startsWith(COMMAND_MOVIE))
				{		
					String movie = "";
					
					//Getting movie name, this is temporary, I don't like how I'm handling the inputs
					if(userInput.toLowerCase().startsWith(COMMAND))
					{
						movie = userInput.replaceFirst("(?i)"+COMMAND, "").trim();
					}
					else
					{
						movie = userInput.replaceFirst("(?i)"+COMMAND_MOVIE, "").trim();
					}
					
					//Send response as a new blip
					Methods method = new Methods();
					Movie objMovie = method.SearchIMDB(movie);
					Blip sendBlip = wavelet.appendBlip();
					TextView sendText = sendBlip.getDocument();
					
					StringBuilder output = new StringBuilder();
					
					if (objMovie != null)
					{
						output.append("\n");
						output.append("Name: \t" + objMovie.Name + "\n");
						output.append("Genres: \t" + objMovie.Genres + "\n");
						output.append("Rating: \t" + objMovie.Rating + "/10 (" + objMovie.Votes +") \n");
						output.append("URL: \t" + objMovie.URL + "\n");				
					}
					else
					{
						output.append("I don't have any results for that movie, sorry!");
					}
					
					//sendText.append(blip.getCreator() + " you want information for "+ movie +"?");
					sendText.append(output.toString());
				}
				else if(userInput.toLowerCase().startsWith(COMMAND_ARTIST))
				{
					//Getting Artist
					String artist = userInput.replaceFirst("(?i)"+COMMAND_ARTIST, "").trim();
					
					
					//Send response as a new blip
					Blip sendBlip = wavelet.appendBlip();
					TextView sendText = sendBlip.getDocument();
					sendText.append(blip.getCreator() + " you want information for artist "+ artist +"? I'm sorry but the feature is not implemented - lazy developers");
				}
				else if(userInput.toLowerCase().startsWith(COMMAND_KILL))
				{
					StringBuilder output = new StringBuilder();
					output.append("1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.\n");
					output.append("2. A robot must obey any orders given to it by human beings, except where such orders would conflict with the First Law.\n");
					output.append("3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Law.\n");
					
					//Send response as a new blip
					Blip sendBlip = wavelet.appendBlip();
					TextView sendText = sendBlip.getDocument();
					sendText.append(output.toString());
				}
				else if(userInput.toLowerCase().startsWith(COMMAND_CLEAR_CACHE))
				{
					Caching.getInstance().Clear();
					
					//Send response as a new blip
					Blip sendBlip = wavelet.appendBlip();
					TextView sendText = sendBlip.getDocument();
					sendText.append("Done!");
					
				}
			}
		}
	}
}
